const user =require("../model/user")
const crypto=require("crypto");//密码加密第三方模块

module.exports=function(req,res){
    var obj=req.body;
    user.findOne({userName:obj.username}).then(result=>{
        if(result){
            obj.password=crypto.createHash('sha256').update(obj.password).digest("hex");
            if(result.password===obj.password){
                req.session.ifLogin=true;
                req.session.user=result;
                res.send({
                    code:0,
                    message:"登陆成功"
                })
            }else{
                res.send({
                    code:1,
                    message:"密码错误，登陆失败"
                })
            }
        }else{
            res.send({
                code:2,
                message:"该用户未注册，请先注册"
            })
        }
    })
}